<?php
/*
 | ----------------------------------------------------------------------------------
 | Copyright (c) 2016 Suzhou ChaoTeng IOT Technology Co., Ltd. All rights reserved.
 | ----------------------------------------------------------------------------------
 | 苏州超腾物联科技有限公司 保留所有版权  网站:http://www.565tech.com  地址:常熟市湖山路99号
 | ----------------------------------------------------------------------------------
 | Create:  Fusky    |  E-Mail: choggle@565tech.com         |  2019-01-14 13:56:02
 | ----------------------------------------------------------------------------------
 | 
 | Auth模块
 | 
 | 
 */

namespace App\Http\Controllers\admin;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use DB;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;

class AuthController extends BaseController
{
    /**
     * 登陆
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function login(Request $request)
    {
        $user = DB::table('users')
        ->where('phone', $request->get('phone'))
        ->where('password', md5($request->get('password')))->first();

        if($user) {
            $token = md5($user->phone.time());
            DB::table('users')->where('id', $user->id)->update(['token'=>$token]);
            return $this->resJson($token, 200);
        }else {
            return $this->resJson('', 201);
        }
    }

    /**
     * 发送验证码
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function smsCode(Request $request) {
        // TODO 发送验证码
        $id = 1;

        return $this->resJson($id, 200);
    }

    /**
     * 注册用户
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function register(Request $request)
    {
        $user = DB::table('users')
        ->where('phone', $request->get('phone'))->first();

        if($user) {
            return $this->resJson('', 201, '号码已存在');
        } else {
            $id = DB::table('users')->insertGetId([
                'phone'     =>  $request->get('phone'),
                'password'  =>  md5($request->get('password')),
                'token'     =>  md5($request->get('phone').time())
            ]);
            return $this->resJson($request->get('cid'), 200, '注册成功');
        }
    }

    /**
     * 用户信息
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function profile(Request $request)
    {
        $user = $request->user;
        $resData['user'] = $user;

        return $this->resJson($resData);
    }

    /**
     * 用户信息完善
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function userEdit(Request $request)
    {
        $user = $request->user;
        $data['username'] = $request->get('username');
        $data['email'] = $request->get('email');
        $data['avatar'] = $request->get('avatar');
        DB::table('users')->where('id', $user->id)
        ->update($data);

        $user = DB::table('users')->where('id', $user->id)->first();
        return $this->resJson($user);
    }

    /**
     * 修改密码
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function changePwd(Request $request)
    {
        $user = $request->user;
        if(md5($request->get('passwd')) == $user->password) {
            DB::table('users')->where('id', $user->id)->update([
                'password'  =>  md5($request->get('passwd2')),
                'token'     =>  ''
            ]);
            return $this->resJson();
        } else {
            return $this->resJson('', 201, '原密码错误');
        }
    }

    /**
     * 封装返回json数据
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function resJson($resData = '', $code = 200, $msg = 'success')
    {
        return json_encode([
            'data'  =>  $resData,
            'code'  =>  $code,
            'msg'   =>  $msg
        ]);
    }

    /**
     * 七牛云存储
     * @return [type] [description]
     */
    public function upload_qiniu(Request $request)
    {
        $file = $request->file('file');
        $ext = $file->getClientOriginalExtension();  //后缀
        $key =date('Y-m-d-H-i').'-'.substr(md5(time()) , 0, 6).'.'.$ext;

        $accessKey = '';
        $secretKey = '';
        $auth = new Auth($accessKey, $secretKey);
        $bucket = '';
        $domain = '/';
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($token, $key, $file->getRealPath());
        if ($err !== null) {
            return [
                "code" => 201,
                "msg" => $err
            ];
        } else {
            //返回图片的完整URL
            return [
                "code" => 200,
                "data" => $domain.$ret['key'],
                "msg" => "上传成功！"
            ];
        }
    }
}
